﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用

using Gift.Model;
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类Car。
    /// </summary>
    public class DALtbTempCar
    {
        public DALtbTempCar()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbTempCar");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(tbTempCar model)
        {
            string strSql = " if exists(select 1 from tbTempCar where orderCode = @orderCode and goodsId = @goodsId and modelTypeId = @modelTypeId ) "
                            + " begin"
                            + " update tbTempCar set goodsCount = goodsCount + @goodsCount ,goodsPoints = @goodsPoints,updateDate = getdate()"
                            + " where  orderCode = @orderCode and goodsId = @goodsId and modelTypeId = @modelTypeId"
                            + " end "
                            + " else"
                            + " begin"
                            + " insert into tbTempCar(orderCode,goodsId,goodsCount,goodsPoints,updateDate, modelTypeId )"
                            + " values(@orderCode,@goodsId,@goodsCount,@goodsPoints,getdate(),@modelTypeId) ;select @@IDENTITY "
                            + " end ";
            SqlParameter[] parameters = {
					new SqlParameter("@orderCode", SqlDbType.NVarChar,50),
					new SqlParameter("@goodsId", SqlDbType.Int,4),
					new SqlParameter("@goodsCount", SqlDbType.Int,4),
					new SqlParameter("@goodsPoints", SqlDbType.Real,8),
                    new SqlParameter("@modelTypeId", SqlDbType.Int,4),                 };
            parameters[0].Value = model.OrderCode;
            parameters[1].Value = model.GoodsId;
            parameters[2].Value = model.goodsCount;
            parameters[3].Value = model.GoodsPoints;
            parameters[4].Value = model.ModelTypeId;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(tbTempCar model)
        {
            string strSql = "if exists(select 1 from tbTempCar where OrderCode = @orderCode and goodsId = @goodsId and modelTypeId = @modelTypeId ) "
                        + " begin"
                        + " delete from tbTempCar where OrderCode = @orderCode and goodsId = @goodsId and modelTypeId = @modelTypeId; "
                        + " end "
                        + " insert into tbTempCar(orderCode,goodsId,goodsCount,goodsPoints,updateDate,modelTypeId)"
                        + " values(@orderCode,@goodsId,@goodsCount,@goodsPoints,getdate(),@modelTypeId)";
            SqlParameter[] parameters = {
					new SqlParameter("@orderCode", SqlDbType.NVarChar,50),
					new SqlParameter("@goodsId", SqlDbType.Int,4),
					new SqlParameter("@goodsCount", SqlDbType.Int,4),
					new SqlParameter("@goodsPoints", SqlDbType.Int,4),
                    	new SqlParameter("@modelTypeId", SqlDbType.Int,4)
                                        };
            parameters[0].Value = model.OrderCode;
            parameters[1].Value = model.GoodsId;
            parameters[2].Value = model.goodsCount;
            parameters[3].Value = model.GoodsPoints;
            parameters[4].Value = model.ModelTypeId;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public decimal GetSumPriceByProdId(int ProductId, int ProfessionAreaTypeId,string sContents)
        {
            string sSQL = " select sum(c.ProdNumber * c.ProdPrice) as SumPriceByPordId"
                          + " from tbTempCar c join DB_Shoppro s on c.ProductId = s.proid "
                          + " join tbProfessionAreaType p on s.ProfessionAreaTypeId = p.Id"
                          + " where c.ProductId != @ProductId and Contents = @Contents and s.ProfessionAreaTypeId = @ProfessionAreaTypeId ";
            SqlParameter[] parameters = {
					new SqlParameter("@ProductId", SqlDbType.Int)
                    ,new SqlParameter("@ProfessionAreaTypeId", SqlDbType.Int)
                    ,new SqlParameter("@Contents", SqlDbType.NVarChar,100)};
            parameters[0].Value = ProductId;
            parameters[1].Value = ProfessionAreaTypeId;
            SqlDataReader dr = DbHelperSQL.ExecuteReader(sSQL, parameters);
            if (dr.Read())
            {

                decimal dResult =  decimal.Parse(dr[0].ToString() == "" ? "0" : dr[0].ToString());
                dr.Close();
                return dResult;
            }
            else
            {
                dr.Close();
                return 0;
            }
        }



        public decimal GetSpendMoney(string sOrderCode, int ProfessionAreaTypeId)
        {
            string sSQL = "select isnull(sum(ProdNumber * ProdPrice),0) as AmountPrice from "
                        + " (select Id,OrderCode,ProductId,ProdNumber,ProdPrice,UpdateDate from tbTempCar c join DB_Shoppro s on c.ProductId = s.proid "
                        + " where c.OrderCode = @OrderCode and s.ProfessionAreaTypeId = @ProfessionAreaTypeId)a ";
            SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,50)
                    ,new SqlParameter("@ProfessionAreaTypeId", SqlDbType.Int)};
            parameters[0].Value = sOrderCode;
            parameters[1].Value = ProfessionAreaTypeId;
            SqlDataReader dr = DbHelperSQL.ExecuteReader(sSQL, parameters);
            if (dr.Read())
            {

                decimal dResult = decimal.Parse(dr[0].ToString());
                dr.Close();
                return dResult;
            }
            else
            {
                dr.Close();
                return 0;
            }

        }

        public decimal GetSpendTotalPrice(string sOrderCode)
        {
            string sSQL = "select isnull(sum(ProdNumber * ProdPrice),0) as AmountPrice from "
                        + "  tbTempCar  "
                        + " where OrderCode = @OrderCode  ";
            SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,50)};
            parameters[0].Value = sOrderCode;
            SqlDataReader dr = DbHelperSQL.ExecuteReader(sSQL, parameters);
            if (dr.Read())
            {

                decimal dResult = decimal.Parse(dr[0].ToString());
                dr.Close();
                return dResult;
            }
            else
            {
                dr.Close();
                return 0;
            }

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete tbTempCar ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(String sOrderCode,int iGoodsId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete tbTempCar ");
            strSql.Append(" where orderCode = @orderCode and goodsId = @goodsId ");
            SqlParameter[] parameters = {
					new SqlParameter("@orderCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@goodsId", SqlDbType.Int,4) };
            parameters[0].Value = sOrderCode;
            parameters[1].Value = iGoodsId;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public tbTempCar GetModel(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,OrderCode,ProductId,ProdNumber,ProdPrice,UpdateDate from tbTempCar ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            tbTempCar model = new tbTempCar();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.OrderCode = long.Parse( ds.Tables[0].Rows[0]["OrderCode"].ToString());
                if (ds.Tables[0].Rows[0]["ProductId"].ToString() != "")
                {
                    model.GoodsId = int.Parse(ds.Tables[0].Rows[0]["ProductId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProdNumber"].ToString() != "")
                {
                    model.goodsCount = int.Parse(ds.Tables[0].Rows[0]["ProdNumber"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProdPrice"].ToString() != "")
                {
                    model.GoodsPoints = int.Parse(ds.Tables[0].Rows[0]["ProdPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UpdateDate"].ToString() != "")
                {
                    model.UpdateDate = DateTime.Parse(ds.Tables[0].Rows[0]["UpdateDate"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,OrderCode,ProductId,ProdNumber,ProdPrice,UpdateDate ");
            strSql.Append(" FROM tbTempCar ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataTable GetListByOrderCode(string orderCode)
        {
            string sSQL = "select c.orderCode,s.goodsCode,s.goodsId,sum(c.goodsCount) as goodsCount,c.goodsPoints, (sum(c.goodsCount) * c.goodsPoints) as sumGoodsPoints,s.goodsName,c.Contents,"
                            + " isnull((select modelTypeName from tbModelType where modelTypeId = c.modelTypeId),'') as modelTypeName,c.modelTypeId "
                             + " from tbTempCar c join tbGoods s on c.goodsId = s.goodsId "
                              + " where c.orderCode = @OrderCode"
                              + " group by c.orderCode,s.goodsId,c.goodsPoints,s.goodsName,c.Contents,c.modelTypeId,s.goodsCode ";
            SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.BigInt,8)};
            parameters[0].Value = orderCode;
            return DbHelperSQL.Query(sSQL, parameters).Tables[0];
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbTempCar";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

